/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Yahoo;

import API.Item;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author mrbao
 */
public class TreeYahoo extends Item {

    private Vector groups;
    private int index;
    private int count;
    private String[] strDisplay;
    private Vector allString;
    private int start;
    private int end;
    private Image iconCollapsed;
    private Image iconExpanded;
    private Image iconOn;
    private Image iconOff;

    public TreeYahoo(Vector groups, int x, int y, int width, int height) {
        this.groups = groups;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        try {
            iconCollapsed = Image.createImage("/Icon/collapsed.gif");
            iconExpanded = Image.createImage("/Icon/expanded.gif");
            iconOn = Image.createImage("/Icon/on.png");
            iconOff = Image.createImage("/Icon/off.png");
        } catch (Exception e) {
        }
        Init();
    }

    public void setGroups(Vector groups) {
        this.groups = groups;
        Init();
    }

    private void Init() {
        count = this.height / font.getHeight();
        allString = new Vector();
        for (int i = 0; i < groups.size(); i++) {
            Group group = (Group) groups.elementAt(i);
            if (group.isExpanded()) {
                allString.addElement("+" + group.getName());
                for (int j = 0; j < group.getAllNicks().size(); j++) {
                    Nick n = (Nick) group.getAllNicks().elementAt(j);
                    allString.addElement(n.statusType + n.nick);
                }
            } else {
                allString.addElement("-" + group.getName());
            }
        }
        if (allString.size() <= count) {
            strDisplay = new String[allString.size()];
            for (int i = 0; i < allString.size(); i++) {
                strDisplay[i] = allString.elementAt(i).toString();
            }
            return;
        }
        start = 0;
        end = count;
        strDisplay = new String[count];
        for (int i = 0; i < count; i++) {
            strDisplay[i] = allString.elementAt(i).toString();
        }
    }

    public void Paint(Graphics g) {
        int tmpX = x;
        int tmpY = y;
        g.setFont(font);
        for (int i = 0; i < strDisplay.length; i++) {
            String string = strDisplay[i];
            if (i == index) {
                g.setColor(0xff0000);
            } else {
                g.setColor(colorText);
            }
            if (string.startsWith("+")) {
                string = string.substring(1);
                g.drawImage(iconExpanded, tmpX, tmpY + (font.getHeight() - iconExpanded.getHeight()) / 2, 0);
                g.drawString(string, tmpX + iconExpanded.getWidth() + 2, tmpY, 0);
            } else if (string.startsWith("-")) {
                string = string.substring(1);
                g.drawImage(iconCollapsed, tmpX, tmpY + (font.getHeight() - iconCollapsed.getHeight()) / 2, 0);
                g.drawString(string, tmpX + iconExpanded.getWidth() + 2, tmpY, 0);
            } else {
                if (string.startsWith(StatusType.OFFLINE + "")) {
                    g.drawImage(iconOff, tmpX + 10, tmpY, 0);
                } else if (string.startsWith(StatusType.AVAILABLE + "")) {
                    g.drawImage(iconOn, tmpX + 10, tmpY, 0);
                }
                g.drawString(string.substring(1), tmpX + 10 + iconOff.getWidth() + 2, tmpY, 0);
            }
            tmpY += font.getHeight();
        }
    }

    public void KeyPress(int keyCode, int gameCode) {
        switch (gameCode) {
            case Canvas.DOWN:
                if (index < strDisplay.length - 1) {
                    index++;
                } else {
                    SetStringDisplay();
                }
                break;
            case Canvas.UP:
                if (index > 0) {
                    index--;
                } else {
                    SetStringDisplay();
                }
                break;
            case Canvas.FIRE:
                FireKey();
                break;
        }
    }

    private void FireKey() {
        String str = allString.elementAt(start + index).toString();
        System.out.println(str);
        if (str.startsWith("+") || str.startsWith("-")) {
            for (int i = 0; i < groups.size(); i++) {
                Group group = (Group) groups.elementAt(i);
                if (group.getName().equals(str.substring(1))) {
                    group.setExpanded();
                    break;
                }
            }
            Init();
        }
    }

    private void SetStringDisplay() {
        if (start > 0 && index == 0) {
            end--;
            start--;
            int j = 0;
            for (int i = start; i < end; i++) {
                strDisplay[j] = allString.elementAt(i).toString();
                j++;
            }
        }
        if (allString.size() > strDisplay.length && index == count - 1 && end < allString.size()) {
            end++;
            start++;
            int j = 0;
            for (int i = start; i < end; i++) {
                strDisplay[j] = allString.elementAt(i).toString();
                j++;
            }
        }
    }
}
